package com.example.crm.service.impl;

import com.example.crm.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
@Transactional
public class RedisServiceImp implements RedisService {
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public Long getSerialNum() {
        RedisAtomicLong entityIdCounter = new RedisAtomicLong("shop_",redisTemplate.getConnectionFactory());
        Long counter = entityIdCounter.incrementAndGet();
        if ((null == counter || counter.longValue() == 1)) {// 初始设置过期时间
            //设置过期时间为1天
            entityIdCounter.expire(1, TimeUnit.DAYS);// 单位天
        }
        return counter;

    }
}
